<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div>zhouwenhua</div>
    <div>amumu</div>
    <script>
        let divs = document.querySelectorAll('div');
        // Array.from操作dom元素
        Array.from(divs,(item)=>{
            item.style.backgroundColor = 'red';
            console.log(item.innerHTML);
        })

        // 展开语法
        let arr1 = ['zhangsan','lisi'];
        let arr2 = ['wangwu','zhaoliu'];
        // 数组拼接
        let arr = [...arr1,...arr2];
        console.table(arr);

        for(let item of arr2){
            arr1.push(item); // 将arr2的每一项追加到arr1中
        }
        console.table(arr1);

        // 扩展 参数
        function sum(...params){
           return params.reduce((prev,next)=>{
               return prev+=next;
            },0)
        }
         console.log(sum(1,2,3,4));

        //  divs.map(item=>{
        //      console.log(item);
        //  })

         // 增加元素
         function rangeArray(begin,end){
            const arr = [];
            for(let i=begin;i<=end;i++){
                arr.push(i)
            }
            return arr;
         }
         console.table(rangeArray(3,5));

         let newArr = ['a','b','c','d'];
         // 从前面添加
         newArr.unshift('e');
         // 从后面删除
         console.table(newArr.pop());
         //从前面删除
         console.table(newArr.shift());
         // 数组填充
         const newArr1 = new Array(5);
         console.log(newArr1);//[empty × 5]
         newArr1.fill('zh');
         console.log(newArr1);//["zh", "zh", "zh", "zh", "zh"]

         const newArr2 = ['a','b','c','d'];
         // slice 截取 不改变原来数组
         console.log(newArr2.slice(0)); // 从0的位置截取到最后 ["a", "b", "c", "d"]
         console.log(newArr2.slice(1,3)); // 从0的位置截取到3不包含3的位置 ["b", "c"]

         // splice
         newArr2.splice(0,1,'zh'); // 从0删除一个后，增加一个zh
         newArr2.splice(1,2,'ha'); // 从0删除两个后，增加一个zh
         console.table(newArr2);

         function move(arr,from,to){
             if(from<0 || to>=arr.length){
                console.error('参数错误');
                return ;
             }
            const newArr = [...arr];
            const item = newArr.splice(from,1); // 将要移动的数组截取出来
            newArr.splice(to,0,...item); // 将截取出来的数组放到要移动的位置
            return newArr;
         }
         console.table(move([1,2,3,4],0,2));

         // 数组排序
         let a1 = [1,5,4,2,6,9,3];
         a1 = a1.sort((a,b)=>{
            return b-a;
         })
         console.table(a1);

         // sort原理
         function sortArray(arr,cb){
             for (const n in arr) {
                 for (const m in arr) {
                     if(cb(arr[n],arr[m])<0){
                        const temp = arr[n];
                        arr[n] = arr[m];
                        arr[m] = temp;
                     }
                 }
             }
             return arr;
         }
         
         let a2 = [1,5,3,7,11,2,4,9];
         a2 = sortArray(a2,(a,b)=>{
             return a-b;
         })
         console.table(a2);
    </script>
</body>
</html>